package vn.edu.saigontech.ncms.model.bookstore;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.OptimisticLockType;

import vn.edu.saigontech.ncms.model.BaseEntity;
import vn.edu.saigontech.ncms.model.user.User;

@Entity
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true, optimisticLock = OptimisticLockType.ALL)
@Table(name = "ncms_bookstore_return")
@SuppressWarnings("serial")
public class Return extends BaseEntity {
	
	@Temporal(TemporalType.DATE)
	@Column(name = "date", nullable = false)
	private Date date;

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "zeturn")
	@org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
	@OrderBy
	private Set<ReturnDetail> details;

	@ManyToOne
	@JoinColumn(name = "user_id", nullable = false)
	private User returner;

	@Column(name = "comment", length = 4000)
	private String comment;
	
	public List<ItemTitle> getItemTitles() {
		List<ItemTitle> ret = new ArrayList<ItemTitle>();
		for(ReturnDetail detail : details) {
			ItemTitle title = detail.getItem().getItemTitle();
			if(!ret.contains(title)) {
				ret.add(title);
			}
		}
		return ret;
	}
	
	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Set<ReturnDetail> getDetails() {
		return details;
	}

	public void setDetails(Set<ReturnDetail> details) {
		this.details = details;
	}

	public User getReturner() {
		return returner;
	}

	public void setReturner(User returner) {
		this.returner = returner;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}
}